using Godot;
using System;

public partial class AxeAbilityController : Node
{
	[Export] 
	PackedScene axeAbilityScene;
	GameEvents gameEvents;//自动加载文件

	int baseDamage = 10;//伤害
	float additionalDamagePercent = 1;

	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		gameEvents = GetNode<GameEvents>("/root/GameEvents");
		gameEvents.AbilityUpgradeAdded += OnAbilityUpgradeAdded;
		GetNode<Timer>("Timer").Timeout += OnTimerTimeout;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}

	public void OnTimerTimeout(){
		Node2D player = GetTree().GetFirstNodeInGroup("player") as Node2D;
		if(player == null){
			return ;
		}
		Node foreground = GetTree().GetFirstNodeInGroup("foreground_layer") as Node2D;
		if(foreground == null){
			return ;
		}
		AxeAbility axeInstance = axeAbilityScene.Instantiate() as AxeAbility;
		foreground.AddChild(axeInstance);
		axeInstance.GlobalPosition = player.GlobalPosition;
		axeInstance.hitboxComponent.damage = this.baseDamage * this.additionalDamagePercent;
	}

	public void OnAbilityUpgradeAdded(AbilityUpgrade upgrade,int quantity){
		if(upgrade.id == "axe_damage"){
			this.additionalDamagePercent = 1 + (float)(quantity * .1);
		}
	}

	public override void _ExitTree(){
		//gameEvents 为自动加载类，不会销毁，所以绑定在gameEvents上的事件需要再节点退出的时候也要解除事件
		gameEvents.AbilityUpgradeAdded -= OnAbilityUpgradeAdded;
		base._ExitTree();
	}
}
